<?php
include_once dirname(__FILE__).'/../RDBConfig.php';
include_once dirname(__FILE__).'/../../interface/AttributeDAOInterface.php';
include_once dirname(__FILE__).'/../../object/Attribute.php';

class AttributeDAO implements AttributeDAOInterface {
	
	public static $TABLE_NAME = 'ATTRIBUTE';
	public static $COL_ATTRIBUTE_PK  = 'ATTRIBUTE_PK';
	public static $COL_ATTRIBUTE_NAME = 'ATTRIBUTE_NAME';
	public static $COL_ATTRIBUTE_VALUE = 'ATTRIBUTE_VALUE';
	
	public function __construct() {
	
	}
	
	/**
	 * Them mot dong vao 'ATTRIBUTE'.
	 * Tra ve so dong da them
	 * @see AttributeDAOInterface::insert()
	 * @param Attribute $attr
	 * @return int
	 */
	public function insert($attr) {
		$table = self::$TABLE_NAME;
		$colAttrPK = self::$COL_ATTRIBUTE_PK;
		$colAttrName = self::$COL_ATTRIBUTE_NAME;
		$colAttrValue = self::$COL_ATTRIBUTE_VALUE;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        INSERT INTO $table
        	($colAttrPK, $colAttrName, $colAttrValue)
                VALUES (:attr_pk, :attr_name, :attr_value)
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':attr_pk' => $attr->getAttributePK(),
				':attr_name' => $attr->getAttributeName(),
				':attr_value'=> $attr->getAttributeValue()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Xoa mot dong co PK tuong ung.
	 * Tra ve so dong da xoa.
	 * @see AttributeDAOInterface::delete()
	 * @param Attribute $attr
	 * @return int
	 */
	public function delete($attr) {
		$table = self::$TABLE_NAME;
		$colAttrPK = self::$COL_ATTRIBUTE_PK;
		$colAttrName = self::$COL_ATTRIBUTE_NAME;
		$colAttrValue = self::$COL_ATTRIBUTE_VALUE;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        DELETE FROM $table
        	WHERE $colAttrPK = :attr_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':attr_pk' => $attr->getAttributePK()				
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da them
	}
	
	/**
	 * Cap nhat mot dong co PK = $old->PK (khong cap nhat cot PK).
	 * Tra ve so dong da cap nhat.
	 * @see AttributeDAOInterface::update()
	 * @param Attribute $old
	 * @param Attribute $new
	 * @return int
	 */
	public function update($old, $new) {
		$table = self::$TABLE_NAME;
		$colAttrPK = self::$COL_ATTRIBUTE_PK;
		$colAttrName = self::$COL_ATTRIBUTE_NAME;
		$colAttrValue = self::$COL_ATTRIBUTE_VALUE;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        UPDATE $table
        	SET
        		$colAttrName = :attr_name,
        		$colAttrValue = :attr_value
        	WHERE $colAttrPK = :attr_pk
END;
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':attr_pk' => $old->getAttributePK(),
				':attr_name' => $new->getAttributeName(),
				':attr_value' => $new->getAttributeValue()
		);
		
		try {
			$conn->beginTransaction(); // bo che do tu dong commit
		
			$sql->execute($data); // do du lieu vao cau truy van
		
			$conn->commit(); // commit de cap nhat thay doi vao CSDL
		
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
			$conn->rollBack();
		}
		
		$conn = null; // free memory
		return $sql->rowCount(); // tra ve so dong da cap nhat
	}
	
	/**
	 * Tra ve mot Attribute co PK tuong ung, neu khong tra ve Attribute(NULL)
	 * @see AttributeDAOInterface::getAttributeByPK()
	 * @param int $attrPK
	 * @return Attribute
	 */
	public function getAttributeByPK($attrPK) {
		$table = self::$TABLE_NAME;
		$colAttrPK = self::$COL_ATTRIBUTE_PK;
		$colAttrName = self::$COL_ATTRIBUTE_NAME;
		$colAttrValue = self::$COL_ATTRIBUTE_VALUE;
		
		$statement = <<<END
        SELECT *
        FROM $table
        WHERE $colAttrPK = :attr_pk
        LIMIT 1
END;
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$data = array( // chuan bi du lieu
				':attr_pk' => $attrPK
		);
		
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			if($sql->rowCount() > 0) {
				$row = $sql->fetch();
				$attr = new Attribute($row["$colAttrPK"], 
						$row["$colAttrName"], $row["$colAttrValue"]);
				return $attr;
			}
			
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
		
		return new Attribute(0, null, null);
	}
}
?>